
package com.tianbo.elfii.client.common;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import javax.imageio.*;
import com.tianbo.elfii.client.common.*;
import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;

/**  <pre>
     <b>类功能描述：</b><pre>
     菜单基础部分，其他菜单继承于此程序
     </pre>
     @version 1.0.1
     @author 高伟 
*/

public class ElfiiCommonBasicMenu {

    JPanel menuPanel;
    JPanel itemPanel;
    JPanel slidePanel;

    public String[] menuItemLabels1;
    public String[] menuItemLabels2;

    BufferedImage unselectedImage;
    BufferedImage selectedImage;
    BufferedImage slideBackgroundImage;
    BufferedImage menuBackgroundImage;

    int selectedNumber=0;
    int labelStartY=0;
    int labelStartMaxY=0;
    int labelStartMinY=0;
    int labelLineHeight=104;

    int slideStartX0=730;
    int slideStartY0=0;
    int slideButtonX=759;
    int slideButtonY=110;
    int slideButtonMinY=110; 
    int slideButtonMaxY=435;

    boolean isSlideMode=true;

    String gameName;

    String topHeadline="";

    //-每个菜单项选择后保存的难度级别
    public int levelGradeMenu1=1,levelGradeMenu2=1,levelGradeMenu3=1,levelGradeMenu4=1,levelGradeMenu5=1,levelGradeMenu6=1,levelGradeMenu7=1,levelGradeMenu8=1,levelGradeMenu9=1,levelGradeMenu10=1,levelGradeMenu11=1,levelGradeMenu12=1,levelGradeMenu13=1,levelGradeMenu14=1;

    public void setTopHeadline(String topHeadline){
        this.topHeadline=topHeadline;
    }

    public String getTopHeadline(){
        return topHeadline;
    }
 
    public void setGameName(String gameName){
        this.gameName=gameName;
    }

    public void setMenuItemLabels(String[] menuItemLabels1,String[] menuItemLabels2){
        this.menuItemLabels1=menuItemLabels1;
        this.menuItemLabels2=menuItemLabels2;
    }

    public void setSelectedNumber(int selectedNumber){
        this.selectedNumber=selectedNumber;
    }

    public JPanel createViewPanel(){

        initMenuItemText();

        JPanel p = new JPanel(new BorderLayout()) ;

        JButton saveDataButton=new JButton();
        saveDataButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    bottomButtonAction(menuItemLabels1[selectedNumber],menuItemLabels2[selectedNumber],true);
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        JButton noDataButton=new JButton();
        noDataButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    bottomButtonAction(menuItemLabels1[selectedNumber],menuItemLabels2[selectedNumber],false);
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        JButton backButton=new JButton();
        backButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealBackButton();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        JButton homeButton=ElfiiCommonRunInfo.getRunInfo().createHomeButton();

        JPanel topPanel=ElfiiCommonUtil.createTopPanel(topHeadline,backButton,homeButton);
        JPanel bottomPanel=ElfiiCommonUtil.createBottomPanelMenu(saveDataButton,noDataButton);
        createMenuPanel();
        p.setLayout(null);
        p.setBackground(Color.black);
        p.add(topPanel);
        topPanel.setBounds(0,0,1024,100);
        p.add(bottomPanel);
        bottomPanel.setBounds(0,668,1024,100);
        p.add(menuPanel);
        menuPanel.setBounds(90,110,832,537);
        return p;
    }

    public void dealBackButton(){
//        ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
        
        ElfiiCommonRunInfo.getRunInfo().setLastSelectedGameName(gameName);
        ElfiiCommonRunInfo.getRunInfo().goBackGameSelect();
    }

    public void createMenuPanel(){

        if (menuItemLabels1.length<5) {
            isSlideMode=false;
            labelLineHeight=520/menuItemLabels1.length;
        }

        if (isSlideMode){
            labelStartMinY=0-(menuItemLabels1.length-5)*100-17;
            if (menuItemLabels1.length==8)
                labelStartMinY=-310;
            if (menuItemLabels1.length==7)
                labelStartMinY=-207;
            if (menuItemLabels1.length==10)
                labelStartMinY=-519;
            if (menuItemLabels1.length==6)
                labelStartMinY=-104;
        }

        if (selectedNumber>=4) {
            labelStartY=labelStartMinY;
            slideButtonY=slideButtonMaxY;
        }

        //-如果selectedNumber==0，应该作为第一次进入处理
        if (ElfiiCommonRunInfo.menuSlideButtonY!=0){
            slideButtonY=ElfiiCommonRunInfo.menuSlideButtonY;
            labelStartY=ElfiiCommonRunInfo.menuLabelStartY;
        }

        File f=null;

        try {
            if (isSlideMode){
                f=new File("resource-common/images/choose 02.png");
                unselectedImage=ImageIO.read(f);
                f=new File("resource-common/images/choose 01.png");
                selectedImage=ImageIO.read(f);
            }else{
                f=new File("resource-common/images/unSelectedMenuItemBGImage.png");
                unselectedImage=ImageIO.read(f);
                f=new File("resource-common/images/selectedMenuItemBGImage.png");
                selectedImage=ImageIO.read(f);
            }
            //- f=new File("resource-common/images/gw/gw_choose_00.png");
            f=new File("resource-common/images/choose 00.png");
            slideBackgroundImage=ImageIO.read(f);
            f=new File("resource-common/images/choose bg nav.png");
            menuBackgroundImage=ImageIO.read(f);
        } catch(Exception e) {System.out.print(e+"  "+f);}

        menuPanel = new JPanel() {
                public void paintComponent(Graphics g) {
                    super.paintComponent(g);

                    Graphics2D g2 = (Graphics2D) g ;
                    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
                    g2.setColor(Color.black);
                    g2.fillRect(0,0,1020,550);
                    g2.setColor(Color.white);
                    g2.fillRoundRect(0,3,832,533,75,75);
                    int textInLabelY=80;

                    if (isSlideMode){
                        textInLabelY=72;
                        g2.drawImage(slideBackgroundImage,null,1,0);
                        //- g2.drawImage(slideBackgroundImage,null,736,0);
                        
                        g2.setStroke(new BasicStroke(7.0f));
                        //g2.drawRoundRect(0,0,832,535,75,75);
                        g2.fillOval(slideButtonX,slideButtonY-25,43,43);
                        g2.setClip(0,18,730,502);
                        
                    }
                    g2.setFont(ElfiiCommonConstant.Menu_Font);
                    String str; int k;
                    for (int i=0; i<menuItemLabels1.length; i++){
                        if (selectedNumber==i)
                            g2.drawImage(selectedImage,null,11,labelStartY+labelLineHeight*i+19);
                        else
                            g2.drawImage(unselectedImage,null,11,labelStartY+labelLineHeight*i+19);
                        g2.setColor(Color.black);
                        str=menuItemLabels1[i];
                        k=str.indexOf(".");
                        if (k>0)
                            str=str.substring(0,k)+str.substring(k+1,str.length());
                        if (str.length()<3)
                            g2.drawString(str,32,textInLabelY+labelStartY+labelLineHeight*i);
                        else
                            g2.drawString(str,25,textInLabelY+labelStartY+labelLineHeight*i);
                        g2.setColor(Color.white);
                        g2.drawString(menuItemLabels2[i],98,textInLabelY+labelStartY+labelLineHeight*i);
                    }
                    g2.drawImage(menuBackgroundImage,null,0,0);

                }
                public Dimension getPreferredSize() {
                    return (new Dimension(832,537));
                }
            };

        menuPanel.setLayout(new BorderLayout());
        menuPanel.setBackground(Color.black);
        menuPanel.setFocusable(true);
        menuPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    dealMousePressed(e);
                }
            }
            );
        menuPanel.addMouseMotionListener(new MouseAdapter(){
                public void mouseDragged(MouseEvent e) {
                    dealMouseDragged(e);
                }
            }
            );
    }

    public void dealMouseDragged(MouseEvent e){
        int x=e.getX();
        int y=e.getY();
        if (isSlideMode && x>slideButtonX && x<slideButtonX+50) {
            slideButtonY=y;
            if (slideButtonY>slideButtonMaxY)
                slideButtonY=slideButtonMaxY;
            if (slideButtonY<slideButtonMinY)
                slideButtonY=slideButtonMinY;
            labelStartY=(int) (labelStartMaxY-(labelStartMaxY -labelStartMinY)*(slideButtonY-slideButtonMinY)/(slideButtonMaxY-slideButtonMinY));

            ElfiiCommonRunInfo.menuSlideButtonY=slideButtonY;
            ElfiiCommonRunInfo.menuLabelStartY=labelStartY;

            menuPanel.repaint();
        }
    }

    public void dealMousePressed(MouseEvent e){
        int x=e.getX();
        int y=e.getY();
        //System.out.println("clicked x="+x+"   x0="+slideStartX0);
        if (x<slideStartX0) {

            for (int i=0; i<menuItemLabels1.length; i++){
                if (y>labelStartY+i*labelLineHeight+10 && y<labelStartY+(i+1)*labelLineHeight+10){
                	if(selectedNumber!=i){
                		selectedNumber=i;
                		ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                	}
                }
            }
            //System.out.println("labelStartY:"+labelStartY);
            
            menuPanel.repaint();
        }
        if (isSlideMode && x>slideButtonX && x<slideButtonX+50) {
            slideButtonY=y;
            if (slideButtonY>slideButtonMaxY)
                slideButtonY=slideButtonMaxY;
            if (slideButtonY<slideButtonMinY)
                slideButtonY=slideButtonMinY;
            labelStartY=(int) (labelStartMaxY-(labelStartMaxY -labelStartMinY)*(slideButtonY-slideButtonMinY)/(slideButtonMaxY-slideButtonMinY));
            //System.out.println("clicked labelStartY:"+labelStartY);

            ElfiiCommonRunInfo.menuSlideButtonY=slideButtonY;
            ElfiiCommonRunInfo.menuLabelStartY=labelStartY;
            
            menuPanel.repaint();
        }
    }

    public void bottomButtonAction(String selectedItemLabel1,String selectedItemLabel2,boolean isDataSaved){
        System.out.println("本行信息不应该出现，ElfiiCommonBasicMenu.bottomButtonAction() 需要被覆盖。");
    }

    public void initMenuItemText(){
        System.out.println("本行信息不应该出现，ElfiiCommonBasicMenu.initMenuItemText() 需要被覆盖。");
    }

}
